package com.geovis.emergency.spd.controller.basic;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.controller.BaseController;
import com.geovis.emergency.spd.biz.basic.service.IBasicLabelService;
import com.geovis.emergency.spd.biz.basic.wrapper.BasicLabelWrapper;
import com.geovis.emergency.spd.entity.basic.entity.BasicLabel;
import com.geovis.emergency.spd.entity.basic.pojo.vo.LabelVO;
import com.geovis.emergency.spd.entity.pc.pojo.enums.StatusEnum;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;


/**
 * @author Lonsho
 * @version 1.0
 * @date 2022/3/16
 */
@RestController
@RequestMapping("/basic/label")
@RequiredArgsConstructor
@Api(value = "基础模块--标签管理", tags = "基础模块--标签管理")
@Slf4j
@Validated
public class BasicLabelController extends BaseController<IBasicLabelService> {

    @GetMapping("/list")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "标签列表", notes = "传入basicLabel")
    public Result<List<BasicLabel>> listAll(BasicLabel basicLabel) {
        return Result.ok(getList(basicLabel));
    }
    @GetMapping("/groupByList")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "带分组标签列表", notes = "传入basicLabel")
    public Result<List<LabelVO>> list(BasicLabel basicLabel) {
        return Result.ok(BasicLabelWrapper.build().labelVO(getList(basicLabel)));
    }

    @PostMapping("/save")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "新增分组", notes = "传入basicLabel")
    public Result save(@RequestBody @Valid BasicLabel basicLabel) {
        LambdaQueryWrapper<BasicLabel> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BasicLabel::getName,basicLabel.getName());
        if (baseService.count(wrapper) > 0){
            return Result.failed(StrUtil.format("【{}】标签已经存在了，请勿重复创建",basicLabel.getName()));
        }
        if (ObjectUtil.isNull(basicLabel.getGroupId())){
            basicLabel.setGroupId(StatusEnum.FALSE.getCode());
        }
        return Result.ok(baseService.save(basicLabel));
    }

    @PostMapping("/update")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "更新标签", notes = "传入basicLabel")
    public Result update(@RequestBody @Valid BasicLabel basicLabel) {
        LambdaQueryWrapper<BasicLabel> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BasicLabel::getName,basicLabel.getName());
        wrapper.ne(BasicLabel::getId,basicLabel.getId());
        if (baseService.count(wrapper) > 0){
            return Result.failed(StrUtil.format("【{}】标签已经存在了，请勿重复创建",basicLabel.getName()));
        }
        if (ObjectUtil.isNull(basicLabel.getGroupId())){
            basicLabel.setGroupId(StatusEnum.FALSE.getCode());
        }
        return Result.ok(baseService.updateById(basicLabel));
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "批量删除", notes = "批量删除数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "idList", value = "删除id的list", required = true, dataType = "java.util.Set", paramType = "body")})
    @PostMapping("/removeByIdList")
    public Result removeByIdList(@NotNull(message = "删除的id集合不能为空") @RequestBody Set<String> idList) {
        baseService.removeBatchByIds(idList);
        return Result.ok();
    }

    private List<BasicLabel> getList(BasicLabel cfgLabel){
        String name = cfgLabel.getName();
        cfgLabel.setName(null);
        LambdaQueryWrapper<BasicLabel> wrapper = Wrappers.lambdaQuery(cfgLabel);
        if (StrUtil.isNotBlank(name)){
            wrapper.like(BasicLabel::getName,name);
        }
        wrapper.orderByAsc(BasicLabel::getName);
        return baseService.list(wrapper);
    }
}
